package com.corpcredit.util;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.tuckey.web.filters.urlrewrite.utils.Log;

import com.corpcredit.model.Glry;
import com.corpcredit.model.Jbqk;
import com.corpcredit.service.LookupManager;
import com.corpcredit.webapp.action.BasePage;

public class GenerateWord extends BasePage
{
	public static String REPORTS_PATH="/corpcredit/styles/";
	public static SimpleDateFormat dateTime = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");//yyyy.MM.dd HH:mm:ss
	private LookupManager lookupManager;
	private Jbqk jbqk;
	public static String generalTableStyle="style=\"width:100%;height:200px;border:1px solid #33FF00;font-size:16pt;text-align:center;\"";

	public GenerateWord()
	{
		//REPORTS_PATH=getRequest().getSession().getServletContext().getRealPath("doc");
		//REPORTS_PATH=getRequest().getContextPath();
		REPORTS_PATH=this.getClass().getResource("").getPath();
		REPORTS_PATH+="../../../../../../../../../../../corpcredit/credit/report/";
	}
	public void setJbqk( Jbqk jbqk )
	{
		this.jbqk = jbqk;
	}

	public void setLookupManager( LookupManager lookupManager )
	{
		this.lookupManager = lookupManager;
	}

	public List getAllGlrys()
	{
		return lookupManager.findAllByJbqkID(Glry.class,jbqk.getId());
	}
	
	public  void generateWord()
	{
		try{
			File f=new File(REPORTS_PATH+dateTime.format(new Date())+".doc");
			FileOutputStream fos = new FileOutputStream(f,true);

			String []columnStyle=getColumnStyle(new int[]{5,5,10,10,10,10,10,10,10,10,10});
			String glryTable=genta(getAllGlrys(),new String[]{"getUsername","getQtqk","getCsrq","getSfgd","getSjcg","getXb","getXmmc","getZgxl","getZw","getZjhm","getZjlx"},generalTableStyle,new String[]{"getUsername","getQtqk","getCsrq","getSfgd","getSjcg","getXb","getXmmc","getZgxl","getZw","getZjhm","getZjlx"},columnStyle);

			fos.write(glryTable.getBytes());
			fos.write(("size : "+getAllGlrys().size()).getBytes());

			fos.close();
		}catch(Exception es)
		{
			es.printStackTrace();
		}
	}
	
	public String genta(List list,String[] methodName,String generalTableStyle,String []columnName,String[]columnStyle)
	{
		StringBuffer content=new StringBuffer();
		content.append("</br> <center>glry list</center>");
		content.append("<table "+generalTableStyle+">");
		content.append("<tr>");
		for(int i=0;i<columnName.length;i++)
		{
			content.append("<th "+columnStyle[i]+">"+columnName[i]+"</th>");
		}
		content.append("</tr>");
		try {

			for(Object o:list)
			{
				content.append("<tr>");
				for(String name:methodName)
				{
					Method me=o.getClass().getMethod(name, null);
					Object value=me.invoke(o, null);
					content.append("<td>"+value+"</td>");
				}
				content.append("</tr>");
			}

		} catch (SecurityException e) 
		{
			e.printStackTrace();
		} catch (NoSuchMethodException e) 
		{
			e.printStackTrace();
		}catch (IllegalArgumentException e) 
		{
			e.printStackTrace();
		} catch (IllegalAccessException e) 
		{
			e.printStackTrace();
		} catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}
		
		content.append("</table>");
		System.out.println(content);
		return content.toString();

	}
	
	public static String[] getColumnStyle(int[]cwidth)
	{
		String[] cstyle=new String[cwidth.length];
		for(int i=0;i<cwidth.length;i++)
		{
			cstyle[i]="style=\"width:"+cwidth[i]+"%\"";
		}
		return cstyle;
	}
	
	/*public String genGlryTable(List<Glry>glry)
	{
		StringBuffer content=new StringBuffer();
		
		content.append("</br> <center>glry list</center>");
		content.append("<table>");
		for(Glry g:glry)
		{
			content.append("<tr>");
			content.append("<td>"+g.getUsername()+"</td>");
			content.append("<td>"+g.getQtqk()+"</td>");
			content.append("<td>"+g.getCsrq()+"</td>");
			content.append("<td>"+g.getSfgd()+"</td>");
			content.append("<td>"+g.getSjcg()+"</td>");
			content.append("<td>"+g.getXb()+"</td>");
			content.append("<td>"+g.getXmmc()+"</td>");
			content.append("<td>"+g.getZgxl()+"</td>");
			content.append("<td>"+g.getZw()+"</td>");
			content.append("<td>"+g.getZjhm()+"</td>");
			content.append("<td>"+g.getZjlx()+"</td>");
			content.append("</tr>");
		}
		
		content.append("</table>");
		System.out.println(content);
		return content.toString();
	}
	*/
}
